package com.sys.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sys.common.constant.Constant;
import com.sys.mapper.PermissionMapper;
import com.sys.pojo.Permission;
import com.sys.pojo.Role;
import com.sys.pojo.RolePermission;
import com.sys.service.PermissionService;
import com.sys.service.RolePermissionService;
import com.sys.service.UserRoleService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ZJF
 * @since 2021-04-01
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {

    @Resource
    private PermissionMapper permissionMapper;

    @Resource
    private UserRoleService userRoleService;

    @Resource
    private RolePermissionService rolePermissionService;

    @Resource
    private RedisTemplate redisTemplate;

    @Override
    @Transactional
    public List<Permission> getPermission(Integer uId) {
        List<Role> userRoles = userRoleService.getAllRole(uId);
        List<Permission> rolePermission = rolePermissionService.getRolePermission(userRoles);
        String key = "user:"+uId;
        List<Permission> allPermission;
        if (redisTemplate.hasKey(key)) {
            allPermission = (List<Permission>) redisTemplate.opsForValue().get(key);
        } else {
            allPermission = getAllPermission(rolePermission);
            redisTemplate.opsForValue().set(key, allPermission);
        }
        return allPermission;
    }


    public List<Permission> getAllPermission(List<Permission> rolePermission) {

        //拿到菜单的所有数据
        List<Permission> list = rolePermission;
        //存储根节点的菜单，即一级菜单
        List<Permission> rootlist = new ArrayList<>();
        //遍历所有数据，找到根节点菜单
        for (Permission menumDTO :
                list) {
            if (menumDTO.getPid().equals(0)) {
                //找到根节点菜单的时候，寻找这个根节点菜单下的子节点菜单。
                findChilds(menumDTO, list);
                //添加到根节点的列表中
                rootlist.add(menumDTO);
            }
        }
        System.out.println(rootlist.toString());
        return rootlist;
    }

    private void findChilds(Permission root, List<Permission> list) {
        List<Permission> childlist = new ArrayList<>();
        //遍历所有数据，找到是入参父节点的子节点的数据，然后加到childlist集合中。
        for (Permission menum :
                list) {
            if (root.getId().equals(menum.getPid())) {
                childlist.add(menum);
            }
        }
        //若子节点不存在，那么就不必再遍历子节点中的子节点了 直接返回。
        if (childlist.size() == 0) {
            return;
        }
        //设置父节点的子节点列表
        root.setChilds(childlist);
        //若子节点存在，接着递归调用该方法，寻找子节点的子节点。
        for (Permission childs :
                childlist) {
            findChilds(childs, list);
        }
    }

    @Override
    public List<Permission> findList(Permission permission) {
        LambdaQueryWrapper<Permission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(Permission::getDeleted, Constant.DELETED);
        List<Permission> permissions = permissionMapper.selectList(queryWrapper);
        List<Permission> allPermission = getAllPermission(permissions);
        for (Permission p : allPermission) {
            Integer pid = p.getPid();
            p.setPName("总公司");
            if (p.getChilds() != null) {
                for (Permission p2 : p.getChilds()) {
                    Permission permissionName = permissionMapper.selectById(p2.getPid());
                    p2.setPName(permissionName.getPermissionName());
                    if (p2.getChilds() != null) {
                        for (Permission p3 : p2.getChilds()) {
                            Permission permissionName2 = permissionMapper.selectById(p3.getPid());
                            p3.setPName(permissionName2.getPermissionName());
                        }
                    }
                }
            }
        }
        return allPermission;
    }

    @Override
    public List<Permission> findMenuList() {
        LambdaQueryWrapper<Permission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(Permission::getDeleted, Constant.DELETED);
        queryWrapper.eq(Permission::getStatus, 0).or().eq(Permission::getStatus, 1);
        queryWrapper.select(Permission::getId, Permission::getPermissionName);
        return permissionMapper.selectList(queryWrapper);
    }

    @Override
    public int add(Permission permission) {
        permission.setDeleted(Constant.UNDELETED);
        return permissionMapper.insert(permission);
    }

    @Override
    public int delete(Integer id) {
        Permission permission = new Permission();
        permission.setId(id);
        permission.setDeleted(Constant.DELETED);
        return permissionMapper.updateById(permission);
    }

    @Override
    public int updateData(Permission permission) {
        return permissionMapper.updateById(permission);
    }

    @Override
    public List<Permission> getRolePermission(Integer rId) {
        List<RolePermission> oneRolePermission = rolePermissionService.getOneRolePermission(rId);
        List<Permission> list = new ArrayList<>();
        for (RolePermission rolePermission : oneRolePermission) {
            LambdaQueryWrapper<Permission> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Permission::getId, rolePermission.getPermissionId());
            queryWrapper.ne(Permission::getDeleted, Constant.DELETED);
            queryWrapper.select(Permission::getId, Permission::getPid);
//            queryWrapper.select(Permission::getId);
            list.add(permissionMapper.selectOne(queryWrapper));
        }
        return list;
    }
}
